package quantity;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import quantity.Quantity.QuantityType;

import Util.Util;

/**
 * Represents a List of Quantities of books. 
 * @author SJP
 *
 */
public class QuantityList extends ArrayList<Quantity> implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6610675037229224732L;

	/**
	 * Creates a new QuantityList
	 */
	public QuantityList() {
		super();
	}
	
	public Quantity addQuantity(Quantity q) {
		QuantityType qt = q.getType();
		
		if (isEmpty()) {
			Util.printStatusString("empty quantity list");
			add(q);
		}
		else {
			Util.printStatusString("non-empty quantity list");
			for (int i = 0; i < size(); ++i) {
				int val = qt.compareTo(get(i).getType());
				Util.printStatusString("val: "+val);
				if (val < 0) {
					Util.printStatusString("Keep Going...");
				}
				else if (val == 0) {
					Util.printStatusString("same quantity types. sort by location.");
					if (qt.getLocation().compareTo(get(i).getType().getLocation()) < 0) {
						Util.printStatusString("location "+qt.getLocation()+" comes before location "+get(i).getType().getLocation());
						add(i,q);
						return q;
					}
					else {
						Util.printStatusString("location "+qt.getLocation()+" comes after location "+get(i).getType().getLocation());
						add(i+1,q);
						return q;
					}
				}
				else {
					add(i,q);
					return q;
				}	
			}
			add(q);
		}
		return q;
	}
	
	private void writeObject(ObjectOutputStream out) throws IOException {
		out.defaultWriteObject();
	}
	
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
		in.defaultReadObject();
	}
}


/*Type Combos	New Need	New On Hand	New On Order	New Canceled	New received
New Need		New Need				
New On Hand		NC			New On Hand			
New On Order	NC			NC			New On Order		
New Canceled	NC			NC			NULL			New Canceled	
New received	NC			NC			New On Hand		NC				New received

Type Combos			Used Need	Used On Hand	Used On Order	Used Canceled	Used received	Used BB Avail	Used BB Purchase
Used Need			Used Need						
Used On Hand		NC			Used On Hand					
Used On Order		NC			NC				Used On Order				
Used Canceled		NC			NC				NULL			Used Canceled			
Used Received		NC			NC				Used On Hand	NC				Used received		
Used BB Avail		NC			NC				NC				NC				NC				Used BB Avail	
Used BB Purchase	NC			NC				NC				NC				NC				Used BB Avail	Used BB Purchase */

